<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input clearable @keyup.enter.native="handleFilter" style="width: 150px;" class="filter-item" placeholder="请输入卡号" v-model="listQuery.cardNo">
      </el-input>
      <el-input clearable style="width: 150px;" class="filter-item" placeholder="商品名称" v-model="listQuery.goodsName">
      </el-input>
      <el-input clearable style="width: 150px;" class="filter-item" placeholder="兑换码" v-model="listQuery.code">
      </el-input>
      <el-input clearable style="width: 150px;" class="filter-item" placeholder="手机号" v-model="listQuery.phone">
      </el-input>
      <el-select clearable v-model="listQuery.stateCode" placeholder="兑换状态">
        <el-option label="兑换中" value="0"></el-option>
        <el-option label="已完成" value="1"></el-option>
      </el-select>
      <el-button size="small" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='商品图' width="140">
        <template slot-scope="scope">
          <a :href="scope.row.goodsImg">
            <img :src="scope.row.goodsImg" width="100" height="100">
          </a>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center">
        <template slot-scope="scope">
          {{scope.row.goodsName}}
        </template>
      </el-table-column>
      <el-table-column align="center" label='兑换码'>
        <template slot-scope="scope">
          {{scope.row.code}}
        </template>
      </el-table-column>
      <el-table-column align="center" label='卡号'>
        <template slot-scope="scope">
          {{scope.row.cardNo}}
        </template>
      </el-table-column>
      <el-table-column align="center" label='电话'>
        <template slot-scope="scope">
          {{scope.row.phone}}
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center">
        <template slot-scope="scope">
          {{scope.row.integral}}
        </template>
      </el-table-column>
      <el-table-column label="领取方式" align="center" width="120">
        <template slot-scope="scope">
          {{scope.row.typeName}}
        </template>
      </el-table-column>
      <el-table-column label="领取状态" align="center">
        <template slot-scope="scope">
          {{scope.row.stateName}}
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center">
        <template slot-scope="scope">
          {{scope.row.phone}}
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.stateCode == '0'" type="primary" size="mini" @click="sureEdit(scope.row,scope.$index)" plain>完成</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--编辑表单弹窗-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="formsrules" ref="dataForm" :model="temp" label-position="right" label-width="120px" style='margin-left:20px; margin-right: 20px;'>
        <el-form-item label="兑换状态：">
          <el-select v-model="temp.stateCode" placeholder="请选择">
            <el-option label="兑换中" :value="0"></el-option>
            <el-option label="已完成" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑表单弹窗END-->

    <!--分页-->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30,50]"
                     :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
    <!--分页END-->
  </div>
</template>

<script>
  import { getList, sureEdit } from '@/api/commodity/exchangeList'

  export default {
    data() {
      return {
        list: null,
        listLoading: true,
        total: null,
        listQuery: {
          cardNo: '',
          stateCode: '',
          goodsName: '',
          code: '',
          phone: ''
        },
        temp: {
          uuid: undefined,
          stateCode: ''
        },
        dialogStatus: '',
        typeList: undefined,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        dialogFormVisible: false,
        formsrules: {
          name: [{ required: true, message: '请输入', trigger: 'blur' }]
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      // 获取列表
      fetchData() {
        this.listLoading = true
        getList(this.listQuery).then(response => {
          this.list = response.data.content
          this.total = response.data.totalElements
          this.listQuery.size = response.data.size
          this.listLoading = false
        })
      },
      handleFilter() {
        this.fetchData()
      },
      sureEdit(row) {
        this.temp = Object.assign({}, row)
        this.temp.stateCode = 1
        if (this.temp.stateCode === 0) {
          this.temp.stateName = '兑换中'
        } else {
          this.temp.stateName = '已完成'
        }
        const temData = Object.assign({}, this.temp)
        sureEdit(temData).then(() => {
          for (const v of this.list) {
            if (v.id === this.temp.id) {
              const index = this.list.indexOf(v)
              this.list.splice(index, 1, this.temp)
              break
            }
          }
          this.dialogFormVisible = false
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          })
        })
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.fetchData()
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .filter-container{
    padding-bottom: 10px;
  }
</style>
